package com.zslibrary.Tools;

import android.os.SystemClock;
import android.util.Log;

import com.zslibrary.BuildConfig;
import com.zslibrary.serialport.SerialPort;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class SerialportUtil {

    private SerialPort mSerialport = null;
    private Thread receiveThread;
    private InputStream mInputStream = null;
    private OutputStream mOutputStream = null;

    public Boolean  openSerialport(String port,int baudRate) {
        try {
//            mSerialport = new SerialPort(new File("/dev/"+port), 115200, 0);
            mSerialport = new SerialPort();
            mSerialport.openPort(port, baudRate);
            mInputStream = mSerialport.getInputStream();
            mOutputStream = mSerialport.getOutputStream();

            receiveThread = new Thread(new Runnable() {
                int recv_size;
                byte[] buffer = new byte[4096];
                @Override
                public void run() {
                    Log.d("receive data", "receiving...");
                    while (true) {
                        if (receiveThread.isInterrupted()) {
                            break;
                        }

                        try {
                            recv_size = mInputStream.read(buffer);
                            if (recv_size > 0) {
//                                Log.d("receive data", HexBytesUtil.byte2HexStr(buffer, recv_size).toString());
                                receiveListener.onDataReceive(buffer, recv_size);
                            }
                        } catch (IOException e) {
                            Log.d("receive data", e.toString());
                            break;
                        }
                        SystemClock.sleep(1);
                    }
                }
            });
            receiveThread.start();
        } catch (IOException e) {
            Log.e("open serialport", e.toString());
            return false;
        }
        Log.d("open serialport", "success");
        return true;
    }

    public void closeSerialport() {
        try {
            this.mInputStream.close();
            this.mOutputStream.close();
            this.mSerialport.closePort();
            this.receiveThread.interrupt();
        } catch (Exception e) {
            Log.d("close serialport", e.toString());
        }
        if(BuildConfig.DEBUG)
            Log.d("close serialport", "close success");
    }

    public void sendSerialport(String str_data) {
//        Log.d("send data", "string data");
        if (str_data.length() <= 0) {
//            Log.d("send data", "wrong data");
            return;
        }
        try {
            this.mOutputStream.write(HexBytesUtil.HexString2Bytes(str_data));
            this.mOutputStream.flush();
        } catch (Exception e) {
            Log.d("send data", e.toString());
        }
    }

    public void sendSerialport(byte[] bytes_data, int length) {
//        Log.d("send data", "bytes data");
        if ((bytes_data.length < length) || (length <= 0)) {
//            Log.d("send data", "wrong data");
            return;
        }
        byte[] temp = new byte[length];
        System.arraycopy(bytes_data, 0, temp, 0, length);
        try {
            this.mOutputStream.write(temp);
            this.mOutputStream.flush();
        } catch (Exception e) {
            Log.d("send data", e.toString());
        }
    }

    public void sendSerialport(int[] int_data, int length) {
//        Log.d("send data", "int data");
        if ((int_data.length < length) || (length <= 0)) {
//            Log.d("send data", "wrong data");
            return;
        }
        try {
            byte[] temp_arr = new byte[length];
            for (int i = 0; i < length; i++) {
                temp_arr[i] = (byte) int_data[i];
            }
            this.mOutputStream.write(temp_arr);
            this.mOutputStream.flush();
        } catch (Exception e) {
            Log.d("send data", e.toString());
        }
    }

    public static interface OnDataReceiveListender {
        public void onDataReceive(byte[] buffer, int size);
    }

    private OnDataReceiveListender receiveListener = null;

    public void setOnDataReceiveListender(OnDataReceiveListender Listener) {
        receiveListener = Listener;
    }
}
